package ru.technogeek.labs.dreamnote.theme;

import java.util.List;
import ru.technogeek.labs.dreamnote.theme.data.FilterDbAdapter;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import ru.technogeek.labs.dreamnote.exceptions.ThemeInitExceprion;;

public class DreamListLayout extends LinearLayout {

	public DreamListLayout(Context context) throws ThemeInitExceprion {
		super(context);
		InitControl(context);
	
	}
	
	public DreamListLayout(Context context, AttributeSet attrs) throws ThemeInitExceprion {
		super(context, attrs);
		InitControl(context);
	}
	
	private void InitControl(final Context context) throws ThemeInitExceprion {
		ThemesManager.getInstance().readTheme(context);
		
		setOrientation(LinearLayout.VERTICAL);
		
		// Create new layout for the filter buttons 
		LinearLayout btnLayout = new LinearLayout(context);
		btnLayout.setOrientation(LinearLayout.HORIZONTAL);
		
		// Get the list of filters from theme
		List<FilterDbAdapter> filetrsList = ThemesManager.getInstance().getCurrentTheme().getFilters();
		
		// Add buttons to the early create layout
		for(final FilterDbAdapter filter : filetrsList) {
			btnLayout.addView(filter, new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.WRAP_CONTENT,
					LinearLayout.LayoutParams.WRAP_CONTENT));
			
			btnLayout.setOnClickListener(new View.OnClickListener() {			
				public void onClick(View v) {
					//StorageController.getIntsance().sorter(filter.getId());
				}
			});
		}
				
		// Add button layout to the main layout 
		addView
			(btnLayout, new LinearLayout.LayoutParams
					(LinearLayout.LayoutParams.FILL_PARENT,
					LinearLayout.LayoutParams.WRAP_CONTENT));
		
		ListView dreamlist = new ListView(context);
		dreamlist.setId(android.R.id.list);
		
		addView
			(dreamlist, new LinearLayout.LayoutParams
					(LinearLayout.LayoutParams.FILL_PARENT, 
					LinearLayout.LayoutParams.WRAP_CONTENT));
		
	}
}
